<!--
 * @author wangpeizhi
 学习记录
 -->
<template>
  <div class="course-record">
    <el-collapse-transition>
      <div v-show="searchshow" class="common-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input v-model.trim="listQuery.subjectName" clearable placeholder="按课件名称进行搜索" class="search-input" @keyup.enter.native="handleFilter" />
        <div class="common-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
          <span class="common-search-more" @click="searchshow = !searchshow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>
        </div>
      </div>
    </el-collapse-transition>
    <el-collapse-transition>
      <div v-show="!searchshow" class="common-search separate">
        <div class="common-search-inner">
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="search-inner-item">
                <div class="item-label">姓名</div>
                <el-input v-model.trim="search.userName" clearable class="filter-item" style="width: 100%;" placeholder="请输入真实姓名搜索" />
              </div>
            </el-col>
            <el-col :span="12">
              <div class="search-inner-item">
                <div class="item-label">课件名称</div>
                <el-input v-model.trim="search.subjectName" clearable class="filter-item" style="width: 100%;" placeholder="请输入课件名称搜索" />
              </div>
            </el-col>
          </el-row>
        </div>
        <div class="common-search-btn">
          <el-button type="primary" @click="handleFilter">{{ $t('table.search') }}</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
          <span class="common-search-more" @click="searchshow = !searchshow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </div>
      </div>
    </el-collapse-transition>
    <div class="btn-container">
      <div class="btn-container-title">
        <span>学习记录</span>
      </div>
    </div>
    <div class="classify-tab">
      <el-tabs v-model="activeTab" @tab-click="handleTabClick">
        <el-tab-pane v-for="(item, n) in statusOptions" :key="n" :label="item.name" :name="item.value" />
      </el-tabs>
    </div>
    <div class="table">
      <my-table :list="list" :filter-column="filtercolumn" tag="courseRecord" operationwidth="80" :list-loading="listLoading" />
    </div>
    <div class="btn-pagion fix">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
  </div>
</template>

<script>
import { queryPage } from '@/views/skill/course/api/record'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import MyTable from '@/components/Table'
import permission from '@/directive/permission/index.js'
export default {
  name: 'CourseRecord',
  components: { Pagination, MyTable },
  directives: { permission },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        subjectName: ''
      },
      list: [],
      total: 0,
      multipleSelection: [],
      listLoading: false,
      searchshow: true,
      search: {
        subjectName: '',
        userName: ''
      },
      activeTab: 'SP',
      statusOptions: [
        {
          name: '视频课程',
          value: 'SP'
        },
        {
          name: '音频课程',
          value: 'YP'
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 选项卡切换
     */
    handleTabClick(tab, event) {
      this.listQuery.page = 1
      this.getList()
    },
    /**
       * 格式化学习时长
       * @param t: 秒
       * @returns {string}
       */
    fomatterTime(t) {
      const fTime = t || 0
      let time = parseInt(fTime) + '秒'
      if (parseInt(fTime) > 60) {
        var second = parseInt(fTime) % 60
        var min = parseInt(fTime / 60)
        time = min + '分' + second + '秒'

        if (min > 60) {
          min = parseInt(fTime / 60) % 60
          var hour = parseInt(parseInt(fTime / 60) / 60)
          time = hour + '小时' + min + '分' + second + '秒'

          if (hour > 24) {
            hour = parseInt(parseInt(fTime / 60) / 60) % 24
            var day = parseInt(parseInt(parseInt(fTime / 60) / 60) / 24)
            time = day + '天' + hour + '小时' + min + '分' + second + '秒'
          }
        }
      }
      return time
    },
    filtercolumn(scope, item) {
      if (item === 'learnDuration') {
        return this.fomatterTime(scope.row[item])
      } else {
        return scope.row[item]
      }
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    resettingBtn() {
      this.listQuery.page = 1
      this.listQuery.subjectName = ''
      for (const i in this.search) {
        if (this.search['' + i + ''] instanceof Array) {
          this.search['' + i + ''] = []
        } else {
          this.search['' + i + ''] = ''
        }
      }
      this.getList()
    },

    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        type: this.activeTab
      }
      if (this.searchshow) {
        query['subjectName'] = this.listQuery.subjectName
      } else {
        query['subjectName'] = this.search.subjectName
        query['userName'] = this.search.userName
      }
      return new Promise((resolve, reject) => {
        queryPage(query).then(response => {
          this.list = response.data.data.rows || []
          this.total = response.data.data.total || 0
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss">
$dark_gray: #bfced6;
.course-record {
  padding: 10px;
  .common-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .common-search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .common-search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }
    &.merge {
      height: 50px;
      line-height: 50px;
    }
    &.separate {
      .common-search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .common-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }
  .btn-container {
    display: flex;
    height: 38px;
    line-height: 38px;
    margin-top: 10px;
    .btn-container-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }
  .classify-tab {
    width: 100%;
    margin: 10px 0;
    padding: 0 10px;
    background-color: #fff;
    border-radius: 4px;

    .el-tabs__nav-next {
      line-height: 40px;
    }

    .el-tabs__nav-wrap::after {
      display: none;
    }

    .el-tabs__header {
      margin: 0;
    }
  }
}
</style>

